package com.service.impl;

import com.bean.entity.Teachers;
import com.bean.pojo.PageAssistant;
import com.bean.vo.TeachersVo;
import com.dao.TeachersMapper;
import com.service.TeachersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
@Service
public class TeachersServiceImpl implements TeachersService {
    @Autowired
    private TeachersMapper teachersMapper;
    @Override
    public TeachersVo teacherLogin(Teachers teachers) throws Exception {

        List<TeachersVo> teachersVoList = teachersMapper.select(teachers);
        if (teachersVoList.size()!=1) throw new Exception("登录失败，请重新输入！");
        return teachersVoList.get(0);
    }

    @Override
    public int save(Teachers t) throws Exception {
        int insert = teachersMapper.insert(t);
        if (insert==1) return 1;
        throw new Exception("老师新增失败，请按照正确方式重新添加！");
    }

    @Override
    public int remove(Teachers t) throws Exception {
        int delete = teachersMapper.delete(t);
        if (delete==1) return 1;
        throw new Exception("老师信息删除失败！");
    }

    @Override
    public int modify(Teachers t) throws Exception {
        int update = teachersMapper.update(t);

        if (update==1) return 1;
        throw new Exception("老师信息更新失败！");

    }

    @Override
    public TeachersVo unique(Teachers t) throws Exception {
        List<TeachersVo> select = teachersMapper.select(t);
        if (select.size()!=1) throw new Exception("老师信息查询失败！");
        return select.get(0);
    }

    @Override
    public List<TeachersVo> query(Teachers t) {
        return teachersMapper.select(t);
    }

    @Override
    public PageAssistant<TeachersVo> queryByPage(Map<String, Object> map, PageAssistant<TeachersVo> pa) {
       //查询并设置总记录数
        pa.setCount(teachersMapper.selectCount(map));

        //添加分页参数
        map.put("startCode",pa.getStartCode());
        map.put("pageSize",pa.getPageSize());

        //查询并添加页面内容
        pa.setItems(teachersMapper.selectByPage(map));

        return pa;
    }
}
